<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>发送房卡</title>
    <link href="/assets/css/manage.css" rel="stylesheet" media="all">
    <script type="text/javascript" src="/files/flexible.js"></script>
    <script type="text/javascript" src="/files/jquery-1.9.1.min.js"></script>
    <script src="/config.js"></script>
</head>
<body>
<div class="head">
    <div class="avatar">
        <img src="" alt="" id="avatar">
    </div>
    <div class="head-right">
        <span id="nickname">昵称: </span>
        <span id="uid">ID: </span>
        <div><span>房卡: </span><span id="num"></span><span>张</span></div>
    </div>
</div>

<div class="input">
    <input style="width:100px" id="search" type="number" name="search" placeholder="用户ID">
    <input style="width:100px" id="card_num" type="number" name="card_num" placeholder="发送数量">
    <button>发送</button>
</div>
<div class="msg"></div>
<script>
    let res = validateLogin();
    $("#nickname").text("昵称: "+res.user.nickname);
    $("#uid").text("ID: " + res.user.id);
    $("#num").text(res.user.card);
    $("#avatar").attr("src",res.user.avatar);
    const clickEvent = (function () {
        if ('ontouchstart' in document.documentElement)
            return 'touchstart';
        else
            return 'click';
    })();

    function msg(msg) {
        let tip = $(".msg");
        tip.html(msg);
        tip.show();
        setTimeout(function () {
            tip.hide();
        }, 3000);
    }

    $(document).on(clickEvent, ".input button", function () {
        let uid = $(".input input[name='search']").val().trim();
        if (uid.length <= 0) {
            msg('请输入用户ID');
            return false;
        }
        let c_num = $(".input input[name='card_num']").val().trim();
        if (c_num.length <= 0) {
            msg('请输入房卡数量');
            return false;
        }
        zn_ajax("/index/SendCard/send", {
            'uid': uid,
            'c_num': c_num
        }, function (e) {
            if (e.code === 1) {
                $(".input input[name='search']").val('');
                $(".input input[name='card_num']").val('');
            }
            msg(e.msg);

        });
    });

    //123
    function zn_ajax(url, data, callback) {
        $.ajax({
            type: "post",
            url: JONO_HOST + url,
            dataType: 'JSON',
            data: data,
            async: false,
            success: callback,
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                msg(errorThrown);
            },
            beforeSend : function(request) {
                request.setRequestHeader("Authorization",window.localStorage.getItem("token"));
            },
        });
    }
</script>
</body>
</html>